package com.tom;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class Post {
	String userid;
	String nickname;
	String title;
	String content;
	String useridmsg;
	String nicknamemsg;
	String titlemsg;
	String contentmsg;
	
	public boolean validate(){
		boolean valid = true;
		
		if (userid.length() < 4 || userid.length() > 20) {
			valid = false;
			useridmsg = "帳號長度不符合!!";
		}
		
		if (nickname.length() < 4 || nickname.length() > 20) {
			valid = false;
			nicknamemsg = "暱稱長度不符合!!";
		}
		
		if (title.length() == 0) {
			valid = false;
			titlemsg = "主題要輸入!!";
		}
	
		if (content.length() == 0){
			valid = false;
			contentmsg = "留言內容要輸入!!";
		}
		
		if (valid) {
									
			int rowcount=0;
			try {
				DriverManager.registerDriver(new Driver());
				Connection conn = DriverManager.getConnection(
						"jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
				String sql = "INSERT INTO guestbook (userid, nickname, title, content) VALUES (?, ?, ?, ?)";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, userid);
				pstmt.setString(2, nickname);
				pstmt.setString(3, title);
				pstmt.setString(4, content);
				
				//System.out.println(sql);
				rowcount = pstmt.executeUpdate();
				System.out.println(rowcount);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if (rowcount != 1){
				}
			}
			
		}
	return valid;
	}
	
	public Post() {
	}
	
	public Post(String userid, String nickname, String title, String content) {
		super();
		this.userid = userid;
		this.nickname = nickname;
		this.title = title;
		this.content = content;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getUseridmsg() {
		return useridmsg;
	}

	public void setUseridmsg(String useridmsg) {
		this.useridmsg = useridmsg;
	}

	public String getNicknamemsg() {
		return nicknamemsg;
	}

	public void setNicknamemsg(String nicknamemsg) {
		this.nicknamemsg = nicknamemsg;
	}

	public String getTitlemsg() {
		return titlemsg;
	}

	public void setTitlemsg(String titlemsg) {
		this.titlemsg = titlemsg;
	}

	public String getContentmsg() {
		return contentmsg;
	}

	public void setContentmsg(String contentmsg) {
		this.contentmsg = contentmsg;
	}
	
	
}
